﻿using Bottle.CommunityForum.Common;
using Bottle.CommunityForum.Factory;
using Bottle.CommunityForum.IBLL;
using Bottle.CommunityForum.Model;
using Bottle.CommunityForum.Model.Enum;
using Bottle.CommunityForum.UIPortal.Models;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Bottle.CommunityForum.UIPortal.Controllers
{
    public class UserController : BaseController
    {
        //
        // GET: /User/

        public IRoleInfoService RoleServcie
        {
            get { return StaticBLLFactory.GetCurrentRoleInfoService(); }
        }

        public IUserInfoService UserServcie
        {
            get { return StaticBLLFactory.GetCurrentUserInfoService(); }
        }

        public ActionResult Index()
        {
            ViewData.Model = RoleServcie.QueryEntitys(s => true);
            return View();
        }

        public ActionResult UserLogin()
        {
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login(UserInfo Model)
        {
            if (!ModelState.IsValid)
            {
                return Json(new { code = delError, msg = "请输入正确格式！" });
            }
            string valiCode = Request["vercode"];

            string sessionCode = Session["VerifyCode"] as string;

            if (string.IsNullOrEmpty(valiCode) || !sessionCode.Contains(valiCode))
            {
                return Json(new { code = delError, msg = "验证码错误！" });
            }
            int delAdmin = (int)UserEnum.Admin;
            var userInfo = UserServcie.QueryEntitys(u => u.email == Model.email && u.userpwd == Model.userpwd)
                .Select(u => new
                {
                    u.userid,
                    u.username,
                    u.userpwd,
                    IsAdmin = u.role
                            .Select(r => r.roleid == delAdmin)
                            .FirstOrDefault()
                }
                )
                .FirstOrDefault();

            if (userInfo == null)
            {
                return Json(new { code = delError, msg = "用户名或密码错误！" });
            }
            Session["VerifyCode"] = "";
            //保存用户登录身份票证
            AspFormsAuthentication.SetFormsAuthentication(userInfo.username, true, JsonConvert.SerializeObject(userInfo));

            return Json(new { code = delSuccess, msg = "ok" });
        }

        public ActionResult LogOut()
        {
            AspFormsAuthentication.logOut();
            return RedirectToAction("Index", "Home");
        }

        public ActionResult Create()
        {
            return View();
        }

        [HttpPost]
        public ActionResult Create(RoleInfo entity)
        {
            if (ModelState.IsValid)
            {
                RoleServcie.AddEntity(entity);
            }
            return RedirectToAction("Index");
        }

    }
}
